package com.shujiaotong.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shujiaotong.common.BaseContext;
import com.shujiaotong.common.R;
import com.shujiaotong.mapper.UserClassMapper;
import com.shujiaotong.mapper.UserMapper;
import com.shujiaotong.pojo.Classes;
import com.shujiaotong.mapper.ClassesMapper;
import com.shujiaotong.pojo.User;
import com.shujiaotong.pojo.UserClass;
import com.shujiaotong.service.IClassesService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shujiaotong.service.IUserClassService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author author
 * @since 2024-10-20
 */
@Service
public class ClassesServiceImpl extends ServiceImpl<ClassesMapper, Classes> implements IClassesService {

    @Autowired
    private UserClassMapper userClassMapper;

    @Autowired
    private UserMapper userMapper;
//        通过传入的课程id，查询userclass表里面对应的userid（不止一个），
//        最后通过userid查询user表的内容，并且把userclass的classname列加入到查询出来的list里面
    public R getUserByClassId(Long id){
        // 查询 UserClass 表的内容
        LambdaQueryWrapper<UserClass> qw = new LambdaQueryWrapper<>();
        qw.eq(UserClass::getClassId, id);
        List<UserClass> userClasses = userClassMapper.selectList(qw);
        List<User> users = new ArrayList<>();
        for (UserClass userClass : userClasses) {
            Long userId = userClass.getUserId();
            String classname = userClass.getClassName();
            User user = userMapper.selectById(userId);
            if (user != null) {
                user.setClassName(classname);
                users.add(user);
            }
        }
        return R.success(users);
    }


    @Override
    public boolean updateAllById(Classes classes) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getName, classes.getTname());
        User user = userMapper.selectOne(qw);
        if (user != null) {
            classes.setTeacherId(user.getId());
            boolean updateResult = super.updateById(classes);
            return updateResult;
        } else {
            return false;
        }
    }


    @Override
    public boolean saveAll(Classes classes) {
        LambdaQueryWrapper<User> qw = new LambdaQueryWrapper<>();
        qw.eq(User::getName, classes.getTname());
        User user = userMapper.selectOne(qw);
        if (user != null) {
            classes.setTeacherId(user.getId());
            boolean saveResult = super.save(classes);
            return saveResult;
        } else {
            return false;
        }

    }

    @Override
    public List<String> getClassByCardTags(String tags) {
//        把tags标签撕裂
        String[] tagNames = tags.split(",");
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
//        投影查询
        qw.select(Classes::getName);
//        条件拼接
        for (String tagName : tagNames) {
            qw.like(Classes::getTags,tagName).or();
        }
        List<String> classNames = this.listObjs(qw);

        return classNames;
    }

    @Autowired
    private IUserClassService userClassService;

    @Override
    public List<Classes> getClassListByTag(String tag) {
        // 1. 获取用户ID
        Long userId = BaseContext.getCurrentId();
        // 2. 根据userId查询userClass表 -> 课程Id
        LambdaQueryWrapper<UserClass> qw1 = new LambdaQueryWrapper<>();
        qw1.select(UserClass::getClassId);
        qw1.eq(UserClass::getUserId,userId);
        List<Long> ucIds = userClassService.listObjs(qw1);

        // 2. 查询 排除 用户已选课程ID
        LambdaQueryWrapper<Classes> qw = new LambdaQueryWrapper<>();
        qw.like(Classes::getTags,tag);
        // 排除用户已选课程  null ""  []
        qw.notIn(!StringUtils.isEmpty(ucIds)&&
                        ucIds.size()!= 0
                ,Classes::getId,ucIds);
        List<Classes> list = this.list(qw);
        return list;
    }
}
